<template>
  <!-- 筛选区 -->
  <div class="sub-filter">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="item in filterData.brands"
          :key="item.id"
          :class="{ active: filterData.brands.selected === item.id }"
          @click="changeBrand(item)"
        >
          {{ item.name }}
        </a>
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="sub in item.properties"
          :key="sub.id"
          :class="{ active: item.selected === sub.id }"
          @click="changeSale(item, sub.id)"
        >
          {{ sub.name }}
        </a>
      </div>
    </div>
  </div>
</template>
<script>
import { findSubCategoryFilter } from '@/api/category'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const route = useRoute()
    const filterData = ref({})
    watch(() => route.params.id, async value => {
      if (!route.path.includes('/sub')) return
      const res = await findSubCategoryFilter(value)
      res.result.brands.unshift({ id: null, name: '全部' })
      // 默认选中品牌的id
      res.result.brands.selected = null

      res.result.saleProperties.forEach(item => {
        item.properties.unshift({ id: null, name: '全部' })
        item.selected = null
      })
      filterData.value = res.result
    }, { immediate: true })

    const getSeletedFilter = () => {
      const filters = {
        brandId: null,
        attrs: []
      }
      const brandId = filterData.value.brands.selected
      if (brandId) {
        filters.brandId = brandId
      }

      filterData.value.saleProperties.forEach(item => {
        if (item.selected) {
          filters.attrs.push({
            groupName: item.name,
            propertyName: item.properties.find(v => v.id === item.selected).name
          })
        }
      })
      return filters
    }
    // 点击品牌的逻辑
    const changeBrand = (item) => {
      filterData.value.brands.selected = item.id
      getSeletedFilter()
      emit('changeFilter', getSeletedFilter())
    }
    // 点击分类属性的逻辑
    const changeSale = (item, id) => {
      item.selected = id
      // console.log(filterData)
      getSeletedFilter()
      emit('changeFilter', getSeletedFilter())
    }
    return {
      filterData,
      changeBrand,
      changeSale
    }
  }
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
